﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//这道题的思路是借助斜率的思想，怎么确定较大值和较小值，只可能发生在斜率发生变化的时候
//前面的斜率pre>0,后面的next<0或者pre<0&&next>0
//考虑有平的地方，pre>=0&&next<0或者pre=<0&&next>0
//考虑只有两个元素的也算数
public class Solution376
{
    public int WiggleMaxLength2(int[] nums)//这个代码存在三个问题
        //1，首尾单独处理
        //2，尾巴处理错误
        //3，pre重复计算
    {
        int pre=0;
        int next = 0;
        int res = 0;

        for (int i = 0; i < nums.Length; i++) {

            //处理头尾
            if (i == 0)
            {
                pre = 0;
                next = nums[i + 1] - nums[i];
            }
            else if (i == nums.Length - 1) {
                pre = nums[i] - nums[i - 1];
                next = 0;
            }
            else
            {
                pre = nums[i] - nums[i-1];
                next = nums[i+1] - nums[i];

            }
            if((pre >= 0 && next < 0)||(pre <= 0 && next > 0))
            {
                res++;
            }


            Console.WriteLine("pre:  "+pre+ "   next:  " + next+ "   res:  " + res);


        }
        return res;

    }
    
    public int WiggleMaxLength(int[] nums)//这个代码解决三个问题
                                           //1，首尾单独处理
                                           //2，尾巴处理错误
                                           //3，pre重复计算
    {
        int pre = 0;
        int next = 0;
        int res = 1;//尾巴默认加1

        for (int i = 0; i < nums.Length-1; i++)
        {

            //处理头尾
            
                
                next = nums[i + 1] - nums[i];

            if ((pre >= 0 && next < 0) || (pre <= 0 && next > 0))
            {
                res++;
                
                pre = next;
            }


            Console.WriteLine("pre:  " + pre + "   next:  " + next + "   res:  " + res);

        }
        return res;

    }
}


class Program376
{

    static void Main376(string[] args)
    {
        //int[] nums = [1, 7, 4, 9, 2, 5];
        int[] nums = [1, 2,2,2,3,4];
        //从1到4整体上坡，所以只有默认的两个摆动
        //1到2 的上坡记录一次就可以了，pre不要变化
        Solution376 solution = new Solution376();
        int res= solution.WiggleMaxLength(nums);
        Console.WriteLine(res);
    }
}